//
//  XZMPublishViewController.m
//  百思不得姐
//
//  Created by 谢忠敏 on 15/7/30.
//  Copyright (c) 2015年 谢忠敏. All rights reserved.
//

#import "XZMPublishViewController.h"
#import "XZMButton.h"
#import "POP.h"
#import "UIView+XZMFrame.h"
#import "MoodViewController.h"
#import "WeatherViewController.h"
#import "JokeViewController.h"
#import "PuzzleViewController.h"
#import "CountGameViewController.h"
#import "PhotoMood.h"
#import "AppDelegate.h"
#import "DateViewController.h"
#define XZMScreenW [UIScreen mainScreen].bounds.size.width
#define XZMScreenH [UIScreen mainScreen].bounds.size.height

@interface XZMPublishViewController ()

//{
//    XZMButton *btn;
//}
@property (nonatomic, weak)UIImageView *imageView;
@property(nonatomic,strong)UIImageView *imageView1;


@end

static NSInteger XZMSpringFactor = 10;
static CGFloat XZMSpringDelay = 0.1;
@implementation XZMPublishViewController
- (IBAction)cancel {
    
//    [self cancelWithCompletionBlock:^{
//        [self dismissViewControllerAnimated:NO completion:nil];
//    }];
    
    
}

//界面有变化时执行
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
   
   [self.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    /** 添加buttom */
    // 数据
    NSArray *images = @[@"测试.png", @"日历.png", @"天气.png", @"游戏.png", @"笑话.png", @"精选.png"];
    NSArray *titles = @[@"测试", @"计算天数", @"天气", @"游戏", @"搞笑", @"精选"];
    
    NSUInteger cols = 3;//每行放几个按钮
    CGFloat btnW = 60;//每个按钮的大小
    CGFloat btnH = btnW + 80;//每个按钮的上下距离
    CGFloat beginMargin = 30;//距离屏幕左右边框的距离
    CGFloat middleMargin = (XZMScreenW - 2 * beginMargin - cols *btnW)/ (cols - 1);
    CGFloat btnStartY = (XZMScreenH - 2 * btnH) * 0.5;
    
    //添加背景图
    [self background];
    
    
    for (int i = 0; i < images.count; i++) {
        
        
        XZMButton *btn = [XZMButton buttonWithType:UIButtonTypeCustom];
        
        NSInteger col = i % cols;
        NSInteger row = i / cols;
        
        CGFloat btnX = col * (middleMargin +btnW) + beginMargin;
        CGFloat btnY = row * btnH + btnStartY;
        
        //把按钮添加到背景图片之上
        [self.imageView1 addSubview:btn];
        
        [btn setTitle:titles[i] forState:UIControlStateNormal];
        
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [btn setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
        
        //  [btn addTarget:self action:@selector(chickBtnDown:) forControlEvents:UIControlEventTouchDown];
        
        [btn addTarget:self action:@selector(chickBtnUpInside:) forControlEvents:UIControlEventTouchUpInside];
        
        btn.tag = i;

//        NSLog(@"%ld",(long)btn.tag);

       

        CGFloat benginBtnY = btnStartY - XZMScreenH;
        
        /** 添加动画 */
        POPSpringAnimation *anima = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        
        anima.fromValue = [NSValue valueWithCGRect:CGRectMake(btnX, benginBtnY, btnW, btnH)];
        
        anima.toValue = [NSValue valueWithCGRect:CGRectMake(btnX, btnY, btnW, btnH)];
        
        anima.springSpeed = XZMSpringFactor;
        
        anima.springBounciness = XZMSpringDelay;
        
        anima.beginTime = CACurrentMediaTime() + i * XZMSpringDelay;
        
        [btn pop_addAnimation:anima forKey:nil];
        
        [anima setCompletionBlock:^(POPAnimation *anima, BOOL finish) {
            
            
        }];
        self.view.userInteractionEnabled = YES;
        
    }
    

//    /** 添加sloganView指示条 */
//    
//    UIImageView *sloganView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"上4"]];
//    
//    sloganView.y = -XZMScreenW;
//    
//    [self.view addSubview:sloganView];
//    
//    CGFloat centerX = XZMScreenW * 0.5;
//
    
    
//    /** 添加sloganView指示条 */
//    UIImageView *sloganView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"上4.jpg"]];
    
//    sloganView.y = -XZMScreenW;
//    
//    [self.view addSubview:sloganView];
//    
//    CGFloat centerX = XZMScreenW * 0.5;
//    
//    CGFloat centerEndY = XZMScreenH * 0.15;
//    
//    CGFloat centerBenginY = centerEndY - XZMScreenH;
    
//    /** 添加动画 */
//    POPSpringAnimation *anima = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
//    anima.fromValue = [NSValue valueWithCGPoint:CGPointMake(centerX, centerBenginY)];
//    anima.toValue = [NSValue valueWithCGPoint:CGPointMake(centerX, centerEndY)];
//    anima.springBounciness = XZMSpringDelay;
//    anima.beginTime = CACurrentMediaTime() + XZMSpringDelay * images.count;
//    
//    anima.springSpeed = XZMSpringFactor;
//    
//    [sloganView pop_addAnimation:anima forKey:nil];
//    
//    [anima setCompletionBlock:^(POPAnimation *anima, BOOL finish) {
//        
//        /** 动画完成后 */
//        self.view.userInteractionEnabled = YES;
//    }];

    
}


- (void)viewDidLoad {
    [super viewDidLoad];

    
//    self.view.backgroundColor = [UIColor cyanColor];
    
    self.view.userInteractionEnabled = NO;
    
    }

- (void)cancelWithCompletionBlock:(void (^)())block
{
    self.view.userInteractionEnabled = NO;
    
    
    int index = 0;
    for (int i = index; i < self.view.subviews.count; i++) {
        UIView *view = self.view.subviews[i];
    
        POPSpringAnimation *anima = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
        
        anima.springBounciness = XZMSpringDelay;
        
        anima.springSpeed = XZMSpringFactor;
        
        anima.beginTime = CACurrentMediaTime() + (i - index) * XZMSpringDelay;
        
        CGFloat endCenterY = view.centerY + XZMScreenH;
        
        anima.toValue = [NSValue valueWithCGPoint:CGPointMake(view.centerX, endCenterY)];
        
        [view pop_addAnimation:anima forKey:nil];
        
        if (i == self.view.subviews.count - 1) { // 最后一个动画完成时
            
            [anima setCompletionBlock:^(POPAnimation *anima, BOOL finish) {
                
                
                
                
                    [self dismissViewControllerAnimated:NO completion:nil];
                
                
                
                
                block();
            }];
        }
        
        
    }
}

- (void)chickBtnDown:(UIButton *)btn
{
    POPBasicAnimation *anima = [POPBasicAnimation animationWithPropertyNamed:kPOPViewScaleXY];
    anima.toValue = [NSValue valueWithCGSize:CGSizeMake(1.1, 1.1)];
    
    [btn pop_addAnimation:anima forKey:nil];
    
}


- (void)chickBtnUpInside:(UIButton *)btn
{
    POPBasicAnimation *anima = [POPBasicAnimation animationWithPropertyNamed:kPOPViewScaleXY];
    
    anima.toValue = [NSValue valueWithCGSize:CGSizeMake(1.3, 1.3)];
    
    [btn pop_addAnimation:anima forKey:nil];
    
    [anima setCompletionBlock:^(POPAnimation *anima, BOOL finish) {
        
        
        POPBasicAnimation *anima2 = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
        
        anima2.toValue = @(0);
        
        [btn pop_addAnimation:anima2 forKey:nil];
        
        [anima2 setCompletionBlock:^(POPAnimation *anima, BOOL finish) {
    
            [self cancelWithCompletionBlock:^{
               // 切换对应控制器
                
                if (btn.tag == 0) {
                   
                    CountGameViewController *gameVC = [[CountGameViewController alloc] init];
                    [self.navigationController pushViewController:gameVC animated:YES];
                }
                
                if (btn.tag == 1) {
                    
                    DateViewController *dataVC = [[DateViewController alloc] init];
                    [self.navigationController pushViewController:dataVC animated:YES];
                    
                }

                if (btn.tag == 2) {
                    
                    WeatherViewController *weaterVC = [[WeatherViewController alloc] init];
                    [self.navigationController pushViewController:weaterVC animated:YES];
                    
                }

                if (btn.tag == 3) {
                    
                    PuzzleViewController *puzzleVC = [[PuzzleViewController alloc] init];
                    [self.navigationController pushViewController:puzzleVC animated:YES];
                    
                }

                if (btn.tag == 4) {
                    
                    JokeViewController *jokeVC = [[JokeViewController alloc] init];
                    [self.navigationController pushViewController:jokeVC animated:YES];
                    
                }

                if (btn.tag == 5) {
                
                    MoodViewController *moodVC = [[MoodViewController alloc] init];
                    [self.navigationController pushViewController:moodVC animated:YES];
                    
                }

                
            }];
        }];
        
    }];
    
   
    

}

- (void)background{
    
    self.imageView1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@""]];
    self.imageView1.frame = CGRectMake(0, 64, ScreenFrame_Widdth, ScreenFrame_Height-100);
    [self.view addSubview:self.imageView1];
    self.imageView1.userInteractionEnabled = YES;
    
    
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    
    //创建搜索请求对象，并且指明所有的数据类型
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"PhotoMood"];
    NSArray *objcArray = [app.managedObjectContext executeFetchRequest:request error:nil];
    
    PhotoMood *photo = objcArray.lastObject;
    
    UIImage *image = [UIImage imageWithData:photo.photo];
    self.imageView1.image = image;
    
    if (objcArray.count == 0) {
        
        self.imageView1.image = [UIImage imageNamed:@"心情.jpg"];
    }
    
    
}

@end
